package com.goldman.aplt.base.task;

import com.goldman.aplt.base.type.TrxnEnvs;
import com.goldman.aplt.util.CommUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
* @Description: 任务基类
* @author zhuwei
* @date 2019年1月15日 下午3:33:51 
**/
@Slf4j
public abstract class TaskBase implements Runnable {

	private TrxnEnvs trxnEnv;

	/**
	 * 必须初始化上下文
	 * @param env
	 */
	public TaskBase(TrxnEnvs env) {
		trxnEnv = env;
	}

	public abstract void process();
	
	/**
	 * 前处理,如有需要子类可覆盖此方法
	 */
	public void runBefor() {
	}
	public void runAfter() {
	}

	@Override
	public void run() {
		long begin = System.currentTimeMillis();
		log.info(">>>>>>>>>>"+this.getClass().getSimpleName() + "run begin.");
		CommUtil.setTrxnEnvs(trxnEnv);
		runBefor();
		process();
		runAfter();
		log.info("<<<<<<<<<<<"+this.getClass().getSimpleName() + "run end, cost {}ms.", System.currentTimeMillis() - begin);
	}
	
	public void start() {
		ThreadPoolBase.getExecutor().execute(this);
	}

	public Future submit() throws ExecutionException, InterruptedException {
		return ThreadPoolBase.getExecutor().submit(this);
	}

}
